package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.GuideAdapter;
import com.hangzhou.xyj.utils.LogUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : neo.duan
 * @date : 	 2016/5/28
 * @desc : 引导页面
 */
public class GuideActivity extends BaseActivity {

    private ViewPager mVpContainer;

    private ViewGroup mVgNavDots;

    private View view1, view2, view3, view4;
    private List<View> views;
    private Bundle bundle;

    @Override
    public void initIntentData(Intent intent) {
        bundle = intent.getBundleExtra(DataKey.BUNDLE);
    }

    @Override
    public void initTop() {
        enableTop(false);
        enableRefresh(false);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_guide);
    }

    @Override
    public void initViews() {
        views = new ArrayList<>();

        mVpContainer = (ViewPager) findViewById(R.id.vp_guide_container);
        view1 = View.inflate(this, R.layout.layout_guide_01, null);
        view2 = View.inflate(this, R.layout.layout_guide_02, null);
        view3 = View.inflate(this, R.layout.layout_guide_03, null);
        view4 = View.inflate(this, R.layout.layout_guide_04, null);
        view4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bundle != null) {
                    Intent i = new Intent(mContext, MainActivity.class);
                    i.putExtra(DataKey.BUNDLE, bundle);
                    mContext.startActivity(i);
                } else {
                    IntentManager.getInstance().goMainActivity(mContext);
                }
                closeActivity();
            }
        });
        views.add(view1);
        views.add(view2);
        views.add(view3);
        views.add(view4);

        mVpContainer.setCurrentItem(0);
        GuideAdapter adapter = new GuideAdapter(views);
        mVpContainer.setAdapter(adapter);
        mVgNavDots = (ViewGroup) findViewById(R.id.rl_guide_nav_dots);
        mVgNavDots.getChildAt(0).setSelected(true);
    }

    @Override
    public void initDatas() {
        CommonPreferences.setShowGuideView(mContext, true);
    }

    @Override
    public void initListeners() {
        mVpContainer.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                LogUtil.d(TAG, "select position ==" + position);
                if (position < mVgNavDots.getChildCount()) {
                    for (int i = 0; i < mVgNavDots.getChildCount(); i++) {
                        View v = mVgNavDots.getChildAt(i);
                        if (v != null) {
                            v.setSelected(i == position);
                        }
                    }
                }
            }

            @Override
            public void onPageScrolled(int position, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
